--
-- @Author:      name
-- @DateTime:    2018-03-30 23:05:48
-- @Description: 消息的处理

-- local skynet = require "skynet"
local log = require "Log"
require "common"
local AssertEx = AssertEx

local CommandBase = class("CommandBase")
---------------------------------------------------------
-- Private
---------------------------------------------------------
function CommandBase:ctor(message,machine)
	self.msg = message 
	self.machine = machine 
	self.close  = false --是否关服
	self.tableid = self.machine:getTableId()
	self.gameid = getGameIdbyTable(self.tableid)
	self:register()
end

--注册本服务里的消息
function CommandBase:register()

	self.msg:registerSelf('safe_close',handler(self,self.safeClose))
	self.msg:registerSelf('update_prop',handler(self,self.updateProperty))
	self.msg:registerSelf('gm_set_code_req',handler(self,self.gmSetCard)) --配牌

	self.msg:registerSelf('enter_table_req',handler(self,self.enterTable))
	self.msg:registerSelf('leave_table_req',handler(self,self.leaveTable))	
	self.msg:registerSelf('lost_connect',handler(self,self.lostConnect))
	self.msg:registerSelf('reconnect_req',handler(self,self.reconnect)) 
	self.msg:registerSelf('sitdown_req',handler(self,self.sitdown))	
	self.msg:registerSelf('standup_req',handler(self,self.standup))	
	self.msg:registerSelf('chat_req',handler(self,self.chat))	

end

---------------------------------------------------------
-- CMD
---------------------------------------------------------
--更新属性
function CommandBase:updateProperty(playerid, data)
	print("_________________大厅发来玩家数据___",data)
	self.machine:updateProperty(playerid,data)
	return true
end

--安全关服
function CommandBase:safeClose(close)
	self.close = close
	self.machine:setClose(close)
end

---------------------------------------------------------
-- REQUEST
---------------------------------------------------------
--进入桌子
function CommandBase:enterTable(playerid,playerInfo)
	AssertEx(not self.close, 'enter_table_res' , 7) --游戏在维护
	AssertEx(playerid > 0 , 'enter_table_res' , 8) --玩家id错误
	local player = self.machine:getPlayer(playerid)
	if player then		
		if player.disconnect then --断线重连进来						
		else
			--没有断线重复请求进入桌子	
			log.debug(">>>>>没有断线重复请求进入桌子________",playerid,self.tableid)
		end
	else
		--第一次进入
		player = table.copy(playerInfo)
		self.machine:addPlayer(player)	
		-- --符合条件，自动坐下
		-- local bSeat = self.machine:sitDown(playerid)
		-- --座位已满
		-- AssertEx(bSeat , 'enter_table_res' , 18)			
	end
	player.disconnect = false
	local result = {
		enter_table_res = {
			tableid = self.tableid,
			gameid = self.gameid,			
		}
	}
	return result
end

--坐下
function CommandBase:sitdown(playerid,seatid)
	-- --符合条件，自动坐下
	local id, err = self.machine:sitDown(playerid,seatid)
	--座位已满
	print("---------------------id",id, err)
	AssertEx(id , 'sitdown_res' , err and err.err_no or 27)	
	local player = self.machine:getPlayer(playerid)
	player.ready = true --准备
	return {
		sitdown_res = {
			seatid = id,
		}
	}			
end

--站起
function CommandBase:standup(playerid)
	-- --符合条件，自动坐下
	local res = self.machine:sitUp(playerid)
	--座位已满
	AssertEx(res , 'standup_res' , 28)
	return {
		standup_res = {
			tableid = self.tableid,
		}
	}			
end

--离开
function CommandBase:leaveTable(playerid)
	local player = self.machine:getPlayer(playerid)
	if player then												
		if self.machine:leaveTable(playerid) then 
			return {leave_table_res = {tableid = self.tableid}}
		end		
		--游戏已开始，请在休息时再退出！
		return {leave_table_res = GetErrorTable(9)}
	else
		--未进入房间
		return {leave_table_res = GetErrorTable(10)}		
	end
	return false
end

--掉线
function CommandBase:lostConnect(playerid)
	local kickPlayer = false
	local player = self.machine:getPlayer(playerid)
	-- print("____lostConnect__",player)
	if player then
		player.disconnect = true		
		kickPlayer = self.machine:disconnect(playerid)
	else
		log.error('>>>>>>>>>>>玩家掉线，但游戏中不存在此玩家:',playerid,self.tableid)
	end
	return kickPlayer
end

--重连
function CommandBase:reconnect(playerid,args)
	local result = {}
	local player = self.machine:getPlayer(playerid)
	AssertEx(player , 'reconnect_res', 10)

end

--聊天
function CommandBase:chat(playerid,args)
	local player = self.machine:getPlayer(playerid)
	local data = {
		nt_chat = {
			chat_type = args.chat_type,
			content = args.content,
			playerid = playerid,
			nick = player.nick
		}
	}
	self.machine:sendToAll(data)
	return true
end

--GM命令
function CommandBase:gmSetCard( playerid,cards ,repeatNum)
	-- self.machine:setGmCard(playerid,cards,repeatNum)
end
------------------------------------------------------------------------------------------------
---游戏里的操作
------------------------------------------------------------------------------------------------





return CommandBase